﻿using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Configuration
{
    public class ModBusConfig
    {
        private static String portName = null;
        private static int baudRate = -1;
        private static int parity = -1;
        private static ushort dataBits = 0xff;
        private static int stopBits = -1;
        private static int readTimeOut = -1;

       const String sectionName = "ModBus";

        public static Boolean HasSection
        {
            get
            {
                return InitConfig.hasSection(sectionName);
            }
        }

        public static string PortName {
            get
            {
                if (portName == null)
                {
                    portName = InitConfig.getString(sectionName, "portName", "COM1");
                }
                return portName;
            }
        }
        public static int BaudRate {
            get
            {
                if (baudRate == -1)
                {
                    String str = InitConfig.getString(sectionName, "baudRate", "9600");
                    baudRate = Int32.Parse(str);
                }
                return baudRate;
                //return ConfigurationManager.AppSettings["DataSourceString"];
            }
        }
        public static Parity Parity
        {
            get
            {
                
                if (parity == -1)
                {
                    String str = InitConfig.getString(sectionName, "parity", "0");
                    parity =Int32.Parse(str);
                }
                return (Parity)parity;
            }
                //return ConfigurationManager.AppSettings["DataSourceString"];
        }

        public static ushort DataBits {
            get
            {
               
                if (dataBits == 0xff)
                {
                    String str = InitConfig.getString(sectionName, "dataBits", "8");
                    dataBits = ushort.Parse(str);
                }
                return dataBits;
            }
        }
        public static StopBits StopBits {
            get
            {
                //StopBits _stopBits = StopBits.None;

                if (stopBits == -1)
                {
                    String str = InitConfig.getString(sectionName, "stopBits", "1");
                    stopBits = Int32.Parse(str);
                }
                return (StopBits)stopBits;
            }
        }
        public static int ReadTimeOut
        {
            get
            {

                if (readTimeOut == -1)
                {
                    String str = InitConfig.getString(sectionName, "readTimeOut", "3000");
                    readTimeOut = Int32.Parse(str);
                }
                return readTimeOut;
            }
        }
    }
}
